$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/statis/getSaleStatistics',
        datatype: "json",
        colModel: [
			{ label: '图书名称', name: 'bookName', index: 'bookName', width: 80 },
			{ label: '总销售额', name: 'totalSale', index: 'totalReward', width: 80,formatter: function (value, options, row) {
                return value/100 +"蝉币"}  },
			{ label: '年销售额', name: 'yearSale', index: 'totalReward', width: 80,formatter: function (value, options, row) {
                return value/100 +"蝉币"}  },
			{ label: '月销售额', name: 'monthSale', index: 'totalReward', width: 80,formatter: function (value, options, row) {
                return value/100 +"蝉币"}  },
			{ label: '日销售额', name: 'daySale', index: 'totalReward', width: 80,formatter: function (value, options, row) {
                return value/100 +"蝉币"}  },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        postData:{
            date:_this.nowDate
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var _this;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		saleStatistics: {},
        q: {
            bookname: null
        },
        nowDate:'',
	},
	created(){
		_this=this;
		this.getNowDate()
	},
	methods: {
        // 选中年月日
        getDate:function(val){
            this.choseCheckBox=false
            var this_time=$('input[name="choseDate"]').val();
            var this_datetime=new Date(this_time);
            this.nowDate=this_time.replace(new RegExp("-","gm"),":")
            console.log(this.nowDate)
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'date': this.nowDate
                },
                page: page
            }).trigger("reloadGrid");

        },
        getNowDate() {
            var now = new Date();
            var year = now.getFullYear(); //年
            var month = now.getMonth() + 1; //月
            var day = now.getDate();
            this.nowDate=year+':'+month+':'+day
        },
        educe: function () {
            window.location.href=baseURL+"sys/statis/educe?date="+this.nowDate;

        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.saleStatistics = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.saleStatistics.id == null ? "sys/statis/save" : "sys/statis/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.saleStatistics),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/saleStatistics/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/saleStatistics/info/"+id, function(r){
                vm.saleStatistics = r.saleStatistics;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {
                    'bookname': vm.q.bookname
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});